#include "SceneTest.h"

#include "../../application/d3ddefs.h"
#include "../../application/GameAppImpl.h"

extern	GameAppImpl*	g_pApp;


SceneTest::SceneTest()
{
	Init();
}

SceneTest::~SceneTest()
{
	Release();
}

void SceneTest::Init()
{

}

void SceneTest::Update(float dt)
{

}

void SceneTest::Render()
{
	//	Prepare render 2d

	D3DXMATRIX matProjection;
	D3DXMatrixOrthoLH(&matProjection,(float)g_pApp->GetWidth(),(float)g_pApp->GetHeight(),0.0f,1.0f);
	g_pApp->GetDevice()->SetTransform(D3DTS_PROJECTION,&matProjection);

	D3DXMATRIX	Identity;
	D3DXMatrixIdentity(&Identity);
	g_pApp->GetDevice()->SetTransform(D3DTS_WORLD,&Identity);
	g_pApp->GetDevice()->SetTransform(D3DTS_VIEW,&Identity);


	//	set vertex data
	LPDIRECT3DVERTEXBUFFER9 pTriangleVB = NULL;
	LPDIRECT3DVERTEXBUFFER9 pQuadVB = NULL;
	VOID* pData = NULL;
	D3DVERTEX aTriangle[] = {	
		{-320.0f,-240.0f,0.1f,	0xffff0000},
		{0.0f,240.0f,0.1f,		0xff00ff00},
		{ 320.0f,-240.0f,0.1f,	0xff0000ff},
	};
	

	//	create buffer to save data 
	g_pApp->GetDevice()->CreateVertexBuffer(sizeof(aTriangle),D3DUSAGE_WRITEONLY,D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED,&pTriangleVB,NULL);
	
	pTriangleVB->Lock(0,0,(void**)&pData,0);
	memcpy(pData,aTriangle,sizeof(aTriangle));
	pTriangleVB->Unlock();



	//	set render state
	//D3DXMatrixPerspectiveFovLH(&m_matProjection,m_fFieldOfView,m_fAspectRatio,m_fNearPlane,m_fFarPlane);
	//m_pDirect3DDevice->SetTransform(D3DTS_PROJECTION,&m_matProjection);

	//m_pDirect3DDevice->SetRenderState(D3DRS_AMBIENT,RGB(255,255,255));
	//m_pDirect3DDevice->SetRenderState(D3DRS_LIGHTING,false);
	//m_pDirect3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
	//m_pDirect3DDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);

	//for(unsigned i = 0;i < 8;++i)
	//{
	//	m_pDirect3DDevice->SetSamplerState(i,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
	//	m_pDirect3DDevice->SetSamplerState(i,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
	//	m_pDirect3DDevice->SetSamplerState(i,D3DSAMP_MIPFILTER,D3DTEXF_ANISOTROPIC);
	//	m_pDirect3DDevice->SetSamplerState(i,D3DSAMP_MAXANISOTROPY,m_dwAnisotropy);
	//}
	//m_pDirect3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);




	//	do render
	g_pApp->GetDevice()->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0);
	g_pApp->GetDevice()->BeginScene();

	g_pApp->GetDevice()->SetStreamSource(0,pTriangleVB,0,sizeof(D3DVERTEX));
	g_pApp->GetDevice()->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);


	g_pApp->GetDevice()->EndScene();
	g_pApp->GetDevice()->Present(NULL,NULL,NULL,NULL);

	//	release buffer
	pTriangleVB->Release();
}

void SceneTest::Release()
{

}